package com.qf.sys.controller;

import com.qf.common.R;
import com.qf.entity.sys.SysUser;
import com.qf.sys.param.LoginParam;
import com.qf.sys.service.SysUserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/sys/user")
public class SysUserController {
    @Resource
    private SysUserService sysUserService;
    @PostMapping("/login")
    public R login(@RequestBody LoginParam loginParam, HttpSession session){
        System.out.println(loginParam.getUsername());
        //使用uuid作为token
        //假数据{code:2000,data:{"token":"4346"}}
        SysUser sysUser = sysUserService.login(loginParam);
        session.setAttribute("sysUser",sysUser);
        String token=UUID.randomUUID().toString();
        Map<String,Object> map=new HashMap<>();
        map.put("token",token);

        return R.ok(map);

    }
    @GetMapping("/info")
    public R info(HttpSession session){
        SysUser sysUser =(SysUser) session.getAttribute("sysUser");
        return R.ok(sysUser);
    }
    @PostMapping("/logout")
    public R logout(HttpSession session){
      session.invalidate();
        return R.ok(null);
    }
}
